package com.gungnir.trial.Controller;

import com.gungnir.trial.entity.ThreeRate;
import com.tyrfing.exceptions.TyrfingServiceException;
import com.gungnir.trial.ThreeRateService;
import com.tyrfing.model.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * REST Controller for ThreeRate operations
 */
@RestController
@RequestMapping("/api/three-rate")
public class ThreeRateController {

    private final ThreeRateService threeRateService;


    @Autowired
    public ThreeRateController(ThreeRateService threeRateService) {
        this.threeRateService = threeRateService;
    }

    // 插入一个新的ThreeRate记录
    @PostMapping("/insert")
    public ResponseEntity<String> insertThreeRate(@RequestBody ThreeRate threeRate) {
        try {
            threeRateService.insertThreeRate(threeRate);
            return new ResponseEntity<>("ThreeRate inserted successfully", HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>("Failed to insert ThreeRate", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //进行数据的三率计算，然后插入一条新的ThreeRate记录
    @PostMapping("/count-and-insert")
    public ResponseEntity<String> countAndInsertThreeRate(
            @RequestParam int shouldAttendance,
            @RequestParam int attendanceFaceCount,
            @RequestParam int attendanceCount,
            @RequestParam int frontAttendanceCount,
            @RequestParam int clroFrontSeatCount,
            @RequestParam String classRoomCode,
            @RequestParam String teachingClassCode,
            @RequestParam Long imagShotTime) {
        try {
            threeRateService.countAndInsertThreeRate(shouldAttendance, attendanceFaceCount, attendanceCount, frontAttendanceCount, clroFrontSeatCount, classRoomCode, teachingClassCode, imagShotTime);
            return new ResponseEntity<>("ThreeRate calculated and inserted successfully", HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>("Failed to calculate and insert ThreeRate", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 通过主键（考勤分析时间和教eRate记录室编码）,删除一个ThreeRate记录
    @DeleteMapping("/delete")
    public ResponseEntity<String> deleteThreeRateByPrimaryKey(@RequestParam Long imagShotTime, @RequestParam String classRoomCode) {
        try {
            threeRateService.deleteThreeRateByPrimaryKey(imagShotTime, classRoomCode);
            return new ResponseEntity<>("ThreeRate deleted successfully", HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>("Failed to delete ThreeRate", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 通过主键（考勤分析时间和教eRate记录室编码）,更新一条ThreeRate记录
    @PutMapping("/update")
    public ResponseEntity<String> updateThreeRate(@RequestBody ThreeRate threeRate) {
        try {
            threeRateService.updateThreeRate(threeRate);
            return new ResponseEntity<>("ThreeRate updated successfully", HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>("Failed to update ThreeRate", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 通过起止时间查找ThreeRate记录
    @GetMapping("/find-by-time-range")
    public ResponseEntity<Pagination<ThreeRate>> findThreeRateByTimeRanged(@RequestParam Integer pageSize, @RequestParam Integer pageNum, @RequestParam Long startTime, @RequestParam Long endTime) {
        try {
            Pagination<ThreeRate> results = threeRateService.findThreeRateByTimeRangePaged(pageSize,pageNum,startTime, endTime);
            return new ResponseEntity<>(results, HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 通过教室编码查找ThreeRate记录
    @GetMapping("/find-by-classroom-code")
    public ResponseEntity<Pagination<ThreeRate>> findThreeRateByClassRoomCode(@RequestParam Integer pageSize, @RequestParam Integer pageNum, @RequestParam String classRoomCode) {
        try {
            Pagination<ThreeRate> results = threeRateService.findThreeRateByClassRoomCodePaged(pageSize,pageNum,classRoomCode);
            return new ResponseEntity<>(results, HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 根据教学班号查询ThreeRate记录数组
    @GetMapping("/find-by-teaching-class-code")
    public ResponseEntity<Pagination<ThreeRate>> findThreeRateByTeachingClassCode(@RequestParam Integer pageSize, @RequestParam Integer pageNum, @RequestParam String teachingClassCode) {
        try {
            Pagination<ThreeRate> results = threeRateService.findThreeRateByTeachingClassCodePaged(pageSize,pageNum,teachingClassCode);
            return new ResponseEntity<>(results, HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    // 根据时间范围和教室编码查询ThreeRate记录数组
    @GetMapping("/find-by-time-range-and-classroom-code")
    public ResponseEntity<Pagination<ThreeRate>> findThreeRateByTimeRangeAndClassRoomCode(@RequestParam Integer pageSize, @RequestParam Integer pageNum, @RequestParam Long startTime,
                                                                                    @RequestParam Long endTime,
                                                                                    @RequestParam String classRoomCode) {
        try {
            Pagination<ThreeRate> results = threeRateService.findThreeRateByTimeRangeAndClassRoomCodePaged(pageSize,pageNum,startTime, endTime, classRoomCode);
            return new ResponseEntity<>(results, HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 根据时间范围和教学班号查询ThreeRate记录数组
    @GetMapping("/find-by-time-range-and-teaching-class-code")
    public ResponseEntity<Pagination<ThreeRate>> findThreeRateByTimeRangeAndTeachingClassCode(@RequestParam Integer pageSize, @RequestParam Integer pageNum, @RequestParam Long startTime,
                                                                                        @RequestParam Long endTime,
                                                                                        @RequestParam String teachingClassCode) {
        try {
            Pagination<ThreeRate> results = threeRateService.findThreeRateByTimeRangeAndTeachingClassCodePaged(pageSize,pageNum,startTime, endTime, teachingClassCode);
            return new ResponseEntity<>(results, HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    // 传入教学班号或教室编码查找最新的ThreeRate记录
    @GetMapping("/find-latest")
    public ResponseEntity<ThreeRate> findLatestByTeachingClassCodeOrClassRoomCode(@RequestParam String teachingClassCode, @RequestParam String classRoomCode) {
        try {
            ThreeRate result = threeRateService.findLatestByTeachingClassCodeOrClassRoomCode(teachingClassCode, classRoomCode);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (TyrfingServiceException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
